package com.uspacex.calculate.service;

import com.singularsys.jep.functions.Str;
import com.uspacex.calculate.mapper.AnalysisMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class AnalysisService {

    @Autowired
    AnalysisMapper analysisMapper;

    public Map<String, Object> dubangfenxifa(String date) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> zichanfuzhai = analysisMapper.zichanfuzhaiByDate(date).get(0);
        Map<String, Object> lirun = analysisMapper.lirunByDate(date).get(0);

        Map<String, Object> data = new HashMap<>();

        double jinglirun = replaceUnit((String)lirun.get("retained_profits"));
        double xiaoshoushouru = replaceUnit((String)lirun.get("operation_revenue"));
        double zichanzonge = replaceUnit((String)zichanfuzhai.get("total_assets"));
        double shaoshugudongquanyi = replaceUnit((String)zichanfuzhai.get("total_equity"))
                - replaceUnit((String)zichanfuzhai.get("total_attributable_equity"));
        double fuzhaizonge = replaceUnit((String)zichanfuzhai.get("total_liability"));

        double xiaoshoujinglilv = jinglirun / xiaoshoushouru;
        double zichanzhouzhuanlv = xiaoshoushouru / zichanzonge;
        double shaoshuquanyilv = shaoshugudongquanyi / zichanzonge;
        double zichanfuzhailv = fuzhaizonge / zichanzonge;

        double zichanlirunlv = xiaoshoujinglilv / zichanzhouzhuanlv;
        double quanyichengshu = 1 / (1-shaoshuquanyilv - zichanfuzhailv);

        double jingzichanshouyilv = zichanlirunlv * quanyichengshu;


        data.put("jinglirun", String.format("%.2f", jinglirun * 100));
        data.put("xiaoshoushouru", String.format("%.2f", xiaoshoushouru * 100));
        data.put("zichanzonge", String.format("%.2f", zichanzonge * 100));
        data.put("shaoshugudongquanyi", String.format("%.2f", shaoshugudongquanyi * 100));
        data.put("fuzhaizonge", String.format("%.2f", fuzhaizonge * 100));

        data.put("xiaoshoujinglilv", String.format("%.2f", xiaoshoujinglilv * 100));
        data.put("zichanzhouzhuanlv", String.format("%.2f", zichanzhouzhuanlv * 100));
        data.put("shaoshuquanyilv", String.format("%.2f", shaoshuquanyilv * 100));
        data.put("zichanfuzhailv", String.format("%.2f", zichanfuzhailv * 100));

        data.put("zichanlirunlv", String.format("%.2f", zichanlirunlv * 100));
        data.put("quanyichengshu", String.format("%.2f", quanyichengshu * 100));

        data.put("jingzichanshouyilv", String.format("%.2f", jingzichanshouyilv * 100));


        List<String> warning = new ArrayList<>();

        warning.add("");


        map.put("data", data);

        return map;
    }

    public Map<String, Object> heisedaobimoxing(String date) {
        Map<String, Object> map = new HashMap<>();
        map.put("result", "该企业不存在“黑色破产”风险。");
        return map;
    }

    public Map<String, Object> woerbizhongfenxi(String date) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> zichanfuzhai = analysisMapper.zichanfuzhaiByDate(date).get(0);
        Map<String, Object> xianjinliuliang = analysisMapper.xianjinliuliangByDate(date).get(0);
        Map<String, Object> lirun = analysisMapper.lirunByDate(date).get(0);

        List<Double> values = new ArrayList<>();
        int count = 0;

        //流动资产/ 流动负债
        double var = (replaceUnit((String)zichanfuzhai.get("total_current_assets"))
                / replaceUnit((String)zichanfuzhai.get("total_current_liability"))
        ) / 2;
        if (var > 1) {
            values.add(2.0);
            count += 25;
        }
        else {
            values.add(Double.parseDouble(String.format("%.1f", var)));
            count += Integer.parseInt(String.format("%.0f", var)) * 25;
        }

        //净资产/ 负债合计
        var = (replaceUnit((String)zichanfuzhai.get("total_assets"))
                / replaceUnit((String)zichanfuzhai.get("total_liability"))
        ) / 1.5;
        if (var > 1) {
            values.add(1.5);
            count += 25;
        }
        else {
            values.add(Double.parseDouble(String.format("%.1f", var)));
            count += Integer.parseInt(String.format("%.0f", var)) * 25;
        }

        //资产/ 固定资产
        var = (replaceUnit((String)zichanfuzhai.get("total_assets"))
                / replaceUnit((String)zichanfuzhai.get("total_non_current_assets"))
        ) / 2.5;
        if (var > 1) {
            values.add(2.5);
            count += 15;
        }
        else {
            values.add(Double.parseDouble(String.format("%.1f", var)));
            count += Integer.parseInt(String.format("%.0f", var)) * 15;
        }

        //营业成本/ 存货(表中数据计算不出，指定固定值)
        values.add(7.0);
        count += 8;

        //营业收入/ 应收帐款(表中数据计算不出，指定固定值)
        values.add(5.0);
        count += 8;

        //营业收入/ 固定资产
        var = (replaceUnit((String)lirun.get("operation_revenue"))
                / replaceUnit((String)zichanfuzhai.get("total_non_current_assets"))
        ) / 4;
        if (var > 1) {
            values.add(4.0);
            count += 10;
        }
        else {
            values.add(Double.parseDouble(String.format("%.1f", var)));
            count += Integer.parseInt(String.format("%.0f", var)) * 10;
        }

        //营业收入/ 净资产
        var = (replaceUnit((String)lirun.get("operation_revenue"))
                / (
                        replaceUnit((String)zichanfuzhai.get("total_assets"))
                - replaceUnit((String)zichanfuzhai.get("total_liability"))
            )
        ) / 3;
        if (var > 1) {
            values.add(3.0);
            count += 5;

        } else if (var < 0) {
            values.add(0.0);
            count += 0;
        } else {
            values.add(Double.parseDouble(String.format("%.1f", var)));
            count += Integer.parseInt(String.format("%.0f", var)) * 5;
        }


        map.put("values", values);
        map.put("count", count);

        return map;
    }


    public Map<String, Object> jiaquandafen(String date) {
        Map<String, Object> map = new HashMap<>();
        Map<String, Object> zichanfuzhai = analysisMapper.zichanfuzhaiByDate(date).get(0);
        Map<String, Object> xianjinliuliang = analysisMapper.xianjinliuliangByDate(date).get(0);
        Map<String, Object> lirun = analysisMapper.lirunByDate(date).get(0);

        List<String> warning = new ArrayList<>();

        //财务结构：资产负债率
        double ziChanFuZhaiLv = replaceUnit((String)zichanfuzhai.get("total_assets"))
                / replaceUnit((String)zichanfuzhai.get("total_liability"));
        //资产负债率必须小于70%，最好低于55%；
        if (ziChanFuZhaiLv > 1.0) {
            map.put("caiwujiegou", 1);
            warning.add("企业负债比率过高");
        } else if (0.8 < ziChanFuZhaiLv) {
            warning.add("企业负债比率较高");
            map.put("caiwujiegou", 1);
        } else if (0.7 < ziChanFuZhaiLv) {
            map.put("caiwujiegou", 2);
        } else if (0.55 < ziChanFuZhaiLv) {
            map.put("caiwujiegou", 3);
        } else if (0.4 < ziChanFuZhaiLv) {
            map.put("caiwujiegou", 4);
        } else {
            map.put("caiwujiegou", 5);
        }


        //短期偿债能力  流动比率，在150%~200%较好； 流动比率=流动资产额/流动负债*100%。
        double liudongbilv = replaceUnit((String)zichanfuzhai.get("total_current_assets"))
                / replaceUnit((String)zichanfuzhai.get("total_current_liability"));
        if (liudongbilv > 1.75) {
            map.put("changzhainengli", 5);
        } else if (1.5 < liudongbilv) {
            map.put("changzhainengli", 4);
        } else if (1 < liudongbilv) {
            map.put("changzhainengli", 3);
        } else if (0.75 < liudongbilv) {
            map.put("changzhainengli", 2);
            warning.add("流动比率一般，企业短期偿债有一定风险");
        } else if (0.5 < liudongbilv) {
            map.put("changzhainengli", 1);
            warning.add("流动比率较低，企业短期偿债能力较差");
        } else {
            map.put("changzhainengli", 1);
            warning.add("流动比率过低，企业短期偿债能力差");
        }


        //现金流量  企业经营活动产生的净现金流应为正值
        double jingXianJinLiu = replaceUnit((String)xianjinliuliang.get("net_cash_flow_occurred_in_operating_activities"));
        if (jingXianJinLiu > 0) {
            map.put("xianjinliuliang", 4);
        } else {
            map.put("xianjinliuliang", 2);
            warning.add("企业经营活动产生的净现金流为负");
        }


        //经营能力 总资产周转率的意义：该指标属于资产管理比率，反映总资产的周转速度，周转越快，说明销售能力越强。企业设置的标准值：0.8 销售收入/总资产
        double zongZiChanZhouZhuanLv = replaceUnit((String)xianjinliuliang.get("cash_inflows_from_operating_activities"))
                / replaceUnit((String)zichanfuzhai.get("total_assets"));
        if (zongZiChanZhouZhuanLv > 0.8) {
            map.put("jingyingnengli", 4);
        } else {
            map.put("jingyingnengli", 2);
            warning.add("企业总资产周转率较低，总资产周转速度较慢");
        }


        //经营效益 营业利润率应大于8%，当然指标值越大，表明企业综合获利能力越强。
        // 营业利润率=营业利润/营业收入(商品销售额)×100% =(销售收入-销货成本-管理费-销售费)/销售收入×100%。
        double yingYeLiRunLv = replaceUnit((String)lirun.get("operating_profit"))
                / replaceUnit((String)lirun.get("operation_revenue"));
        if (zongZiChanZhouZhuanLv > 0.8) {
            map.put("jingyingxiaoyi", 4);
        } else {
            map.put("jingyingxiaoyi", 2);
            warning.add("企业总营业利润率较低，企业综合获利能力较差");
        }

        map.put("warning", warning);
        return map;
    }



    private double replaceUnit(String str) {
        double d = 0.0;
        if (str.endsWith("亿")) {
            d = Double.parseDouble(str.replace("亿", ""))*10000;
        } else if (str.endsWith("万")) {
            d = Double.parseDouble(str.replace("万", ""));
        } else {
            d = Double.parseDouble(str);
        }
        return d;
    }

}
